package com.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.health.excel.OrderSettingListener;
import com.health.pojo.OrderSetting;
import com.health.service.OrderSettingService;
import com.health.vo.Result;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 设置可预约人数
     */
    @PostMapping("editNumber")
    public Result editNumber(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumber(orderSetting);

        return new Result(true,"预约人数设置成功");
    }

    /**
     * 查询每月的预约设置人数和已预约人数-数据回显
     */
    @GetMapping("/findByDate")
    public Result findByDate(@DateTimeFormat(pattern = "yyyy-MM") Date date){
        List<Map<String,Integer>> settingList=orderSettingService.findByDate(date);

        return new Result(true,"预约设置查询成功",settingList);
    }


    /**
     * 上传预约设置excel
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        //读取excel文件
        EasyExcel.read(
                excelFile.getInputStream(),
                OrderSetting.class,
                new OrderSettingListener(orderSettingService)
        ).sheet().doRead();
        return new Result(true,"预约设置上传成功");
    }
}
